package com.aaa.bbb.mymaintain.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.aaa.bbb.mymaintain.entity.IssuesEmployee;
import com.aaa.bbb.mymaintain.dao.IssuesEmployeeDao;
import com.aaa.bbb.mymaintain.form.IssueEmployeeAddForm;
import com.aaa.bbb.mymaintain.form.IssuesEmployeeForm;
import com.aaa.bbb.mymaintain.service.IssuesEmployeeSer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.bbb.mymaintain.entity.IssuesEmployee;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author max
 * @since 2023-11-03
 */
@Service
public class IssuesEmployeeSerImpl extends ServiceImpl<IssuesEmployeeDao, IssuesEmployee> implements IssuesEmployeeSer {
    @Override
    public PageInfo<Map<String, Object>> queryPagerByCond(String username,
                                                          int pageNum,
                                                          int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = baseMapper.selectByCond(username);
        String qianzhui="../../../src/assets/";
        for(Map<String, Object> item : list){
            String url = item.get("picture").toString();
            String explanation = item.get("explanation").toString();
            String[] temp;
            String[] temp1;
            String delimiter = ";";// 指定分割字符
            temp = url.split(delimiter);// 分割字符串
            temp1 = explanation.split(delimiter);
            for(int i=0;i<temp.length;i++){
                temp[i] = qianzhui+temp[i];
            }
            item.put("picture",temp);
            item.put("explanation",temp1);
        }

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Map<String, String> queryMessageById(String issue_id) {
        return baseMapper.selectByIssueId(issue_id);
    }

    @Override
    public PageInfo<Map<String, Object>> queryPagerByCond1(String username,String state, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = baseMapper.selectByCond1(username,state);
        String qianzhui="../../../src/assets/";
        for(Map<String, Object> item : list){
            String url = item.get("picture").toString();
            String explanation = item.get("explanation").toString();
            String[] temp;
            String[] temp1;
            String delimiter = ";";// 指定分割字符
            temp = url.split(delimiter);// 分割字符串
            temp1 = explanation.split(delimiter);
            for(int i=0;i<temp.length;i++){
                temp[i] = qianzhui+temp[i];
            }
            item.put("picture",temp);
            item.put("explanation",temp1);
        }
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public boolean checkAndInsert(IssuesEmployee issuesEmployee,IssueEmployeeAddForm form) {
        Map<String, String> issueIdMaxMap = baseMapper.selectMaxIssueId();
        String issueIdMax = issueIdMaxMap.get("maxissueid");
        if(StrUtil.isEmpty(issueIdMax)) {
            issuesEmployee.setIssueId("I0001");
        } else {
            String issueIdMaxNum = issueIdMax.substring(1);
            System.out.println(issueIdMaxNum);
            Integer issueIdMaxNumInt = Integer.parseInt(issueIdMaxNum);
            String lastFix = NumberUtil.decimalFormat("0000", ++issueIdMaxNumInt);
            issuesEmployee.setIssueId("I" + lastFix);
        }
        issuesEmployee.setJobId(baseMapper.selectJobId(form.getUsername()).get("job_id"));
        issuesEmployee.setDetail(form.getDetail());
        issuesEmployee.setExplanation(form.getExplanation());
        issuesEmployee.setPicture(form.getPicture());
        issuesEmployee.setState("问题未审核");
        issuesEmployee.setDate(form.getDate());
        return save(issuesEmployee);
    }

    public void updateIssueEmployee(String issue_id,String state){
        baseMapper.updateState(issue_id,state);
    }

    @Override
    public void maintainUnQualified1(String issue_id, String state, String reason) {
        baseMapper.maintainUnQualified2(issue_id,state,reason);
    }
}
